// pages/member/activityDetail.js
var req = require('../../interfaceImpl.js');
var config = require('../../interfaceConfig.js');
var app = getApp();
var strAppCode = config.SmallApp;
var detail = '';
var buynum = 1;
var finalMoney = 0;
var Money = 0;
var yuanMoney = 0;
var finalyuanMoney = 0;
var resGoodsSign = ''
var id = ''
var reqSaveOrderSign = "";
var code = "";
var UserID = "";
var payresult = "";
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    //判断是否登录
    UserID = app.globalData.cinemapojo.UserID;
    id = options.EventId;
    var that = this;
    that.setData({ buynum: 1 })
    if (app.globalData.hasLogin) { //TODO记得修改
      //TODO获取商品详情
      that.setData({ buynum: buynum })
      return req.GoodsDetail(id).then(res => {
        if (res.data.success) {
          detail = res.data.data;
          app.globalData.tempactivitydetail = detail;
          detail.EventEnd = detail.EventEnd.split("T")[0];
          detail.EventStart = detail.EventStart.split("T")[0]
          console.log(detail)
          Money = finalMoney = detail.PriceSale;
          finalyuanMoney = yuanMoney = finalMoney + detail.EventDiscountValue;
        }
        that.setData({ detail, finalMoney, finalyuanMoney })
        wx.setNavigationBarTitle({
          title: detail.EventName,
        })
        wx.hideLoading();
      }).then(() => {
        return req.CalculateActivityPrice(detail.EventDiscountTarget, detail.EventDiscountType, detail.EventDiscountValue, detail.PriceService, buynum, detail.PriceSale, detail.EventDiscountThreshold).then(res => {
          if (res.data.pricetotal!=="") {
            var danprice = res.data.pricetotal;
            that.setData({ danprice })
          } else {
            wx.showModal({
              title: '提示',
              content: '获取活动价失败。请重试试',
            })
            return false;
          }

        })
      })

    } else {
      wx.login({
        success: function (res) {
          console.log(res)
          code = res.code;
          return req.MiniAppLogin(code).then(res => {
            if (res.data.notRegister) {//TODO记得修改
              wx.reLaunch({
                url: '../regiest/regiest?fromhuodongfood=true&&EventId=' + id + ''
              })
            } else {
              var token = res.data.data
              //TODO请求用户信息
              app.globalData.hasLogin = true;
              app.globalData.token = token;
              return req.GetUserInfo(token).then(res => {
                if (res.data.success) {
                  app.globalData.loginuserinfo = res.data.data // res.data.data
                  that.setData({ phone: app.globalData.loginuserinfo.mobile, hasLogin: true })
                  console.log(app.globalData.loginuserinfo)
                  that.setData({ hasF: true })
                  wx.getUserInfo({
                    success: function (res) {
                      app.globalData.userinfo = res.userInfo
                      console.log(res)
                      that.setData({ userinfo: app.globalData.userinfo })
                    }, fail: function () {
                      //拒绝授权
                    },
                  })


                } else {
                  wx.showModal({
                    title: '提示',
                    content: '登陆失败,请检查网络',
                  })
                  return false;
                }
              }).then(() => {
                return req.GoodsDetail(id).then(res => {
                  if (res.data.success) {
                    detail = res.data.data;
                    app.globalData.tempactivitydetail = detail;
                    detail.EventEnd = detail.EventEnd.split("T")[0];
                    detail.EventStart = detail.EventStart.split("T")[0]
                    console.log(detail)
                    Money = finalMoney = detail.PriceSale;
                    finalyuanMoney = yuanMoney = finalMoney + detail.EventDiscountValue;
                  }
                  that.setData({ detail, finalMoney, finalyuanMoney })
                  wx.setNavigationBarTitle({
                    title: detail.EventName,
                  })
                  wx.hideLoading();
                }).then(() => {
                  return req.CalculateActivityPrice(detail.EventDiscountTarget, detail.EventDiscountType, detail.EventDiscountValue, detail.PriceService, buynum, detail.PriceSale, detail.EventDiscountThreshold).then(res => {

                    if (res.data.pricetotal !=="") {
                      var danprice = res.data.pricetotal;
                      that.setData({ danprice })
                    } else {
                      wx.showModal({
                        title: '提示',
                        content: '获取活动价失败。请重试试',
                      })
                      return false;
                    }

                  })
                })

              })
            }
          })
        }, fail: function (res) {
          wx.showModal({
            title: '提示',
            content: '登陆失败',
          })
        }
      })
    }



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**零食的数量的加**/
  addnum: function (e) {
    var that = this;
    //重置数据
    
    if (detail.EventProductNumLimit ==buynum){
      this.setData({hasMax:true})
      return false;
    }
    buynum++;
    return req.CalculateActivityPrice(detail.EventDiscountTarget, detail.EventDiscountType, detail.EventDiscountValue, detail.PriceService, buynum, detail.PriceSale, detail.EventDiscountThreshold).then(res => {
      if (res.data.pricetotal !== "") {
        var danprice = res.data.pricetotal;
   
     

        that.setData({
          danprice:danprice,
          buynum: buynum,
         
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '获取活动价失败。请重试试',
        })
        return false;
      }

    })
 
    //结束标识符
  },
  nojian: function (e) {

  },
  /**减少购买零食**/
  jiannum: function (e) {
    this.setData({ hasMax: false })
    //重置数据
    var that = this;
    buynum--;
    return req.CalculateActivityPrice(detail.EventDiscountTarget, detail.EventDiscountType, detail.EventDiscountValue, detail.PriceService, buynum, detail.PriceSale, detail.EventDiscountThreshold).then(res => {
      if (res.data.pricetotal !== "") {
        var danprice = res.data.pricetotal;



        that.setData({
          danprice: danprice,
          buynum: buynum,

        })
      } else {
        wx.showModal({
          title: '提示',
          content: '获取活动价失败。请重试试',
        })
        return false;
      }

    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    buynum = 1; this.setData({ buynum })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  payFor: function () {

    var GoodsInfo = id + "|" + buynum;
    //TODO提交卖品
    var goodsSign = "strAppCode=" + strAppCode + "AddTypeID=" + "1" + "FeatureAppNo=" + "PriceTotal=" + finalMoney + "" + "SeatInfo=" + "UserID=" + UserID + "" + "PayPhone=" + app.globalData.loginuserinfo.mobile + "EventID=" + id + "" + "OrderSource=2" + "EventDiscountTotal=" + (detail.PriceSale - detail.EventDiscountValue) * buynum + "" + "Num=" + buynum + "" + "GoodsInfo=" + GoodsInfo + "";
    return req.GetSignStr(goodsSign).then(res => {
      if (res.data.success)
        reqSaveOrderSign = res.data.data
    }).then(() => {
      //提交卖品
      console.log(21342432)
      return req.SaveOrderGoods(strAppCode, "1", "", finalMoney, "", UserID, app.globalData.loginuserinfo.mobile, id, "2", detail.EventDiscountValue, buynum, GoodsInfo, reqSaveOrderSign, app.globalData.token).then(res => {
        console.log(res)
        if (res.data.success) {
          detail.buynum = buynum;
          payresult = res.data.data
          console.log("----------")
          console.log(payresult)
          wx.redirectTo({


            url: '/pages/huodong/foodpayfor?payresult=' + JSON.stringify(payresult)

          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.data,
          })

        }
      })
    })





  },
  hasmax:function(){
    wx.showModal({
      title: '提示',
      content: '超过最大购买量',
    })
  }
})



